﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.DAL
{
    public class StockInDetailDAL:IStockInDetailDAL
    {
        #region IStockInDetailDAL 成员

        public IDataReader Select()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public IDataReader SelectByNum(string StockInNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Reader(ConnectionString.ConnString, "Proc_StockInDetail_Select", new SqlParameter[] { new SqlParameter("@StockInNum", StockInNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public DataTable Select(string StockInNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_StockInDetail_Select", new SqlParameter[] { new SqlParameter("@StockInNum",StockInNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Insert(string StockInNum, int DrugNum, string ProductedArea, DateTime ProductedDatetime, DateTime ExpiryDatetime, float ImportPrice, string BatchNum, int Quantity, float Amount)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@StockInNum",StockInNum),
                    new SqlParameter("@DrugNum",DrugNum),
                    new SqlParameter("@BatchNum",BatchNum),
                    new SqlParameter("@ProductedArea",ProductedArea),
                    new SqlParameter("@ProductedDatetime",ProductedDatetime),
                    new SqlParameter("@ExpiryDatetime",ExpiryDatetime),
                    new SqlParameter("@ImportPrice",ImportPrice),
                    new SqlParameter("@Quantity",Quantity),
                    new SqlParameter("@Amount",Amount)
                };
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInDetail_Insert", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Update(string StockInNum, int DrugNum, string ProductedArea, DateTime ProductedDatetime, DateTime ExpiryDatetime, float ImportPrice, string BatchNum, int Quantity, float Amount)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                     new SqlParameter("@StockInNum",StockInNum),
                    new SqlParameter("@DrugNum",DrugNum),
                    new SqlParameter("@ProductedArea",ProductedArea),
                    new SqlParameter("@ProductedDatetime",ProductedDatetime),
                    new SqlParameter("@ExpiryDatetime",ExpiryDatetime),
                    new SqlParameter("@ImportPrice",ImportPrice),
                    new SqlParameter("@BatchNum",BatchNum),
                    new SqlParameter("@Quantity",Quantity),
                    new SqlParameter("@Amount",Amount)
                };

                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInDetail_Update", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Delete(string StockInNum, int DrugNum)
        {
            try
            {
                SqlParameter [] all=new SqlParameter[]
                {
                    new SqlParameter("@StockInNum",StockInNum),
                    new SqlParameter("@DrugNum",DrugNum)
                };
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInDetail_Delete", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Delete(string StockInNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInDetail_Delete_By_StockInNum", new SqlParameter[] { new SqlParameter("@StockInNum", StockInNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        #endregion
    }
}
